package org.javaforever.cookieshop.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.javaforever.cookieshop.domain.TypeBean;
import org.javaforever.cookieshop.service.TypeService;
import org.javaforever.cookieshop.utils.BooleanUtil;
import org.javaforever.cookieshop.utils.PDFUtil;
import org.javaforever.cookieshop.utils.POIExcelUtil;
import org.javaforever.cookieshop.utils.POIPPTUtil;
import org.javaforever.cookieshop.utils.StringUtil;
import org.javaforever.cookieshop.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "/typeController")
@RestController
public class TypeController {
	public TypeBean tempType = new TypeBean();
	@Autowired
	protected TypeService service;

	@RequestMapping(value = "/activateAllTypes", method = RequestMethod.POST)
	public Map<String,Object> activateAllTypes(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateAllTypes(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/activateType", method = RequestMethod.POST)
	public Map<String,Object> activateType(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.activateType(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/addType", method = RequestMethod.POST)
	public Map<String,Object> addType(@RequestBody TypeBean type) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.addType(type);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/checkAccessType", method = RequestMethod.GET)
	public Map<String,Object> checkAccessType() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneAllTypes", method = RequestMethod.POST)
	public Map<String,Object> cloneAllTypes(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneAllTypes(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/cloneType", method = RequestMethod.POST)
	public Map<String,Object> cloneType(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.cloneType(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteAllTypes", method = RequestMethod.POST)
	public Map<String,Object> deleteAllTypes(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteAllTypes(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/deleteType", method = RequestMethod.POST)
	public Map<String,Object> deleteType(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.deleteType(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/exportPPTTypes")
	public void exportPPTTypes(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.pptx");
            List<TypeBean> list = service.listAllTypes();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportTypes")
	public void exportTypes(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.xls");
            List<TypeBean> list = service.listAllTypes();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportTypesPDF")
	public void exportTypesPDF(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.pdf");
            List<TypeBean> list = service.listAllTypes();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/exportTypesWord")
	public void exportTypesWord(HttpSession session,HttpServletResponse response,HttpServletRequest request) throws Exception{
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.doc");
            List<TypeBean> list = service.listAllTypes();
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterPPTTypes", method = RequestMethod.GET)
	public void filterPPTTypes(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "name", required = false) String name,@RequestParam(value = "active", required = false) String active) throws Exception{
        TypeBean type0 = new TypeBean();
        if (!StringUtil.isBlank(name)) type0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) type0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.pptx");
            List<TypeBean> list = service.searchTypesByFields(type0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIPPTUtil.exportPPTWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterTypesExcel", method = RequestMethod.GET)
	public void filterTypesExcel(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "name", required = false) String name,@RequestParam(value = "active", required = false) String active) throws Exception{
        TypeBean type0 = new TypeBean();
        if (!StringUtil.isBlank(name)) type0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) type0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.xls");
            List<TypeBean> list = service.searchTypesByFields(type0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            POIExcelUtil.exportExcelWorkbookWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterTypesPDF", method = RequestMethod.GET)
	public void filterTypesPDF(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "name", required = false) String name,@RequestParam(value = "active", required = false) String active) throws Exception{
        TypeBean type0 = new TypeBean();
        if (!StringUtil.isBlank(name)) type0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) type0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.pdf");
            List<TypeBean> list = service.searchTypesByFields(type0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            PDFUtil.exportPDFWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/filterTypesWord", method = RequestMethod.GET)
	public void filterTypesWord(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value = "name", required = false) String name,@RequestParam(value = "active", required = false) String active) throws Exception{
        TypeBean type0 = new TypeBean();
        if (!StringUtil.isBlank(name)) type0.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) type0.setActive(activeBool);
        try(OutputStream out = response.getOutputStream()){
            response.addHeader("Content-Disposition", "attachment;filename=Types.doc");
            List<TypeBean> list = service.searchTypesByFields(type0);
            List<List<Object>> contents = new ArrayList<>();
            String sheetName = "类型信息表";
            String [] headers = {"序号","名字","活跃"};
            Boolean [] isImages = {false,false,false};
            
            for (TypeBean type:list) {
                Object [] row = {StringUtil.toNullString(type.getId()),type.getName(),StringUtil.toNullString(type.getActive())};
                contents.add(Arrays.asList(row));
            }
            
            WordUtil.exportWordWithImage(out, sheetName, Arrays.asList(headers), contents, Arrays.asList(isImages));
        }
	}
	@RequestMapping(value = "/findTypeById", method = RequestMethod.POST)
	public Map<String,Object> findTypeById(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        TypeBean type = service.findTypeById(id);
        result.put("success",true);
        result.put("data",type);
        return result;
	}
	@RequestMapping(value = "/findTypeByName", method = RequestMethod.POST)
	public Map<String,Object> findTypeByName(@RequestParam(value = "name", required = true) String name) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        TypeBean type = service.findTypeByName(name);
        result.put("success",true);
        result.put("data",type);
        return result;
	}
	@RequestMapping(value = "/listActiveTypes", method = RequestMethod.POST)
	public Map<String,Object> listActiveTypes() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<TypeBean> list = service.listActiveTypes();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/listAllTypes", method = RequestMethod.POST)
	public Map<String,Object> listAllTypes() throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        List<TypeBean> list = service.listAllTypes();
        result.put("success",true);
        result.put("rows",list);
        return result;
	}
	@RequestMapping(value = "/searchTypesByFieldsByPage", method = RequestMethod.POST)
	public Map<String,Object> searchTypesByFieldsByPage(@RequestParam(value = "page", required = false) Integer pagenum,@RequestParam(value = "rows", required = false) Integer pagesize,@RequestParam(value = "last", required = false) String lastFlag,@RequestParam(value = "name", required = false) String name,@RequestParam(value = "active", required = false) String active) throws Exception{
        TypeBean type = new TypeBean();
        if (!StringUtil.isBlank(name)) type.setName(name);
        Boolean activeBool = BooleanUtil.parseBoolean(active);
        if (activeBool != null) type.setActive(activeBool);
        if (pagesize==null || pagesize <= 0) pagesize = 10;
        Integer recordCount = service.countSearchTypesByFieldsRecords(type);
        Integer pageCount = (int)Math.ceil((double)recordCount/pagesize);
        if (pageCount <= 1) pageCount = 1;
        if (pagenum==null || pagenum <= 1) pagenum = 1;
        if (pagenum >= pageCount) pagenum = pageCount;
        Boolean lastFlagBool = BooleanUtil.parseBoolean(lastFlag);
        if (lastFlagBool == null) lastFlagBool = false;
        if (lastFlagBool) pagenum = pageCount;
        Map<String,Object> result = new TreeMap<String,Object>();
        List<TypeBean> typeList = service.searchTypesByFieldsByPage(type,pagesize,pagenum);
        result.put("success",true);
        result.put("rows",typeList);
        result.put("total",recordCount);
        result.put("page",pagenum);
        return result;
	}
	@RequestMapping(value = "/softDeleteAllTypes", method = RequestMethod.POST)
	public Map<String,Object> softDeleteAllTypes(@RequestParam(value = "ids", required = true) String ids) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteAllTypes(ids);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/softDeleteType", method = RequestMethod.POST)
	public Map<String,Object> softDeleteType(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.softDeleteType(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleOneType", method = RequestMethod.POST)
	public Map<String,Object> toggleOneType(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleOneType(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/toggleType", method = RequestMethod.POST)
	public Map<String,Object> toggleType(@RequestParam Long id) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.toggleType(id);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
	@RequestMapping(value = "/updateType", method = RequestMethod.POST)
	public Map<String,Object> updateType(@RequestBody TypeBean type) throws Exception{
        Map<String,Object> result = new TreeMap<String,Object>();
        service.updateType(type);
        result.put("success",true);
        result.put("data",null);
        return result;
	}
}

